package com.lanou.musicproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.bean.SongListRecomm;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 王世美 on 15/10/24.
 *  推荐页-歌单推荐适配器
 */
public class RecommSongListAdapter extends BaseAdapter {

    // 获取歌单图片(pic),聆听数(listenum),标题(title)
    private List<SongListRecomm.ContentEntity.ListEntity> data;
    // 上下文
    private Context mContext;

    // 有参构造方法
    public RecommSongListAdapter(Context c, List<
            SongListRecomm.ContentEntity.ListEntity> data) {
        this.data = data;
        mContext = c;
    }

    //获得行布局个数
    @Override
    public int getCount() {
        if (data != null && data.size() > 0)
            return data.size();
        else
            return 0;
    }

    //获取行布局item的位置
    @Override
    public SongListRecomm.ContentEntity.ListEntity getItem(int position) {
        if (data != null && data.size() > 0)
            return data.get(position);
        else
            return null;
    }

    // 获取行布局itemId
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 获得行布局内容
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).
                    inflate(R.layout.recomm_gridview_songlist_item, null);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SongListRecomm.ContentEntity.ListEntity list = getItem(position);
        if (list != null) {
            Picasso.with(mContext).load(list.getPic()).into(holder.backgroundIV);
            holder.listenTV.setText(list.getListenum());
            holder.songTitleTV.setText(list.getTitle());
        }


        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'recomm_gridview_songlist_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        // 初始化组件
        @Bind(R.id.backgroundIV)
        ImageView backgroundIV;
        @Bind(R.id.listenTV)
        TextView listenTV;
        @Bind(R.id.songTitleTV)
        TextView songTitleTV;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
